<?php
class Upload_form extends CI_Controller 
{
		
    function __construct()
	{
		parent::__construct();	
		$this->load->model('form_model');
		$this->load->model('student_model');
		$this->load->language('class','english');
	}
	function getPath($action = '')
	{
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index($page = 0)
	{
		$per_page_count 	     = $this->config->item('per_page_count');
		$view 				     = $this->getPath(__FUNCTION__);
		$view['menu_var']         = 2;
		if(isset($_POST['search']))
		{
			$form              = $this->input->post('form');         // filter subject with form 
			$view['filter1']     = $form ;  
			$this->session->set_userdata(array('filter' => $form));    // set filter value
			
		}
		else
		{
			$view['filter1']     = $this->session->userdata('filter');
		}
		if(!empty($view['filter1']))
		{
		$like['name']             = $view['filter1'];   //$form;
		}
		$where                    = array('deleted' => 0);
		$form                   = $this->form_model->getforms($page, $per_page_count , $like, $where );
		$view['form'] 		  = $form->records;
		$view['cur_page'] 	      = $page;
		$view['title']		      = 'Forms List';
		$view['subtitle']	      = 'Forms Lists';
		/************************ Pagination block ***********************************/
		$config['base_url'] 	  = $this->config->item('admin_base_url').'/upload_form/index/';
		$config['total_rows'] 	  = $form->total;
		$config['per_page']       = $per_page_count;
		$config['cur_page']       = $page /$per_page_count;
		$config['uri_segment'] 	  = 4;
		$config['num_links'] 	  = 3;
		$this->pagination->initialize($config);
		$view['msg'] 	          = $this->lang->line($this->session->flashdata('form_msg'));
		$this->layout->adminview('form_view', $view);
	
	}
	function home()
	{
		/**************** Total users ******************************/
		$where                    = array();
		$students                 = $this->student_model->getStudents($page, $per_page_count , $like, $where );
		//$view['students'] 		  = $students->records;
		$view['stud_total']       = $students->total;
		$view['menu_var']         = 1;
		/**************** Total deleted  users ******************************/
		$where                    = array('users.deleted' => 0);
		$students                 = $this->student_model->getStudents($page, $per_page_count , $like, $where );
		$view['students'] 		  = $students->records;
		$view['stud_del_total']       = $students->total;
		
		/**************** Total male users ******************************/
		$where                    = array('users.deleted' => 0,'gender'=>'male');
		$students                 = $this->student_model->getStudents($page, $per_page_count , $like, $where );
		//$view['students'] 		  = $students->records;
		$view['stud_male_total']       = $students->total;
	
		/**************** Total female users ******************************/
		$where                    = array('users.deleted' => 0,'gender'=>'female');
		$students                 = $this->student_model->getStudents($page, $per_page_count , $like, $where );
		//$view['students'] 		  = $students->records;
		$view['stud_female_total']       = $students->total;
		
		/**************** Total student users ******************************/
		$where                    = array('users.deleted' => 0,'type'=>'student');
		$students                 = $this->student_model->getStudents($page, $per_page_count , $like, $where );
		//$view['students'] 		  = $students->records;
		$view['stud_student_total']       = $students->total;
		
		/**************** Total employee users ******************************/
		$where                    = array('users.deleted' => 0,'type'=>'employee');
		$students                 = $this->student_model->getStudents($page, $per_page_count , $like, $where );
		//$view['students'] 		  = $students->records;
		$view['stud_emp_total']       = $students->total;
		
		$where                    = array('deleted' => 0);
		$form                     = $this->form_model->getforms($page, $per_page_count , $like, $where );
		$view['form_total'] 	  = $form->total;
		$this->layout->adminview('home', $view);
	}
	function add($cur_page = 0)
	{
		$view                    = $this->getPath(__FUNCTION__);
		$view['title']           = "form Details";
		$view['menu_var']         = 2;
		$save                    = $this->input->post('save');
		$view['form'] 	     = array();
		$view['cur_page']        = $cur_page;
		$view['subtitle']	     = 'Add form';
		if(!empty($save)) 
		{
			$pdf_file             = $_FILES['file']['name'];
			$ext                  = end(explode('.', $pdf_file));
			$data                 = $this->retrieve->post($this->form_model->fields);
			$insertStatus         = $this->form_model->add($data);
			$view['form']    	  = $data;
		
			//print_r($insertStatus);
			/********************* Image upload************/
			//echo $_FILES['file']['type'];
			if (!($_FILES['file']['type'] =="application/pdf"))
			{
				$view['err_msg'] ="Your uploaded file must be of PDF. Other file types are not allowed<BR>";
			}
			else
			{
			
				$l_image_path           = "fpanel/images/uploads/".$insertStatus->id.".".$ext; // the path with the file name where the file will be stored,  
		
				if(move_uploaded_file ($_FILES['file']['tmp_name'],$l_image_path))
				{
				chmod("$l_image_path",0777);
				}
				else
				{
					echo "Failed to upload file Contact Site admin to fix the problem";
exit;
				}
			}
			
			/**********************image upload ******************************/
			switch($insertStatus->status)
			{
				case 1:
					$this->session->set_flashdata('form_msg', 'con_add_success');
					if(!empty($cur_page)) 
						redirect($this->config->item('admin_base').'upload_form/index/'.$cur_page,'location');
					else
						redirect($this->config->item('admin_base').'upload_form/index/','location');
					exit;		
					break;
				case -1:
					break;
				case -2:
					$view['err_msg'] 	= $this->lang->line('con_valid_failed');
					break;
				case -3:
					$view['err_msg'] 	= $this->lang->line('con_already_exists');
					break;
				case false:
					$view['err_msg'] 	= $this->lang->line('con_add_failure');
	
					break;
				
				default:
					$view['err_msg'] 	= $this->lang->line('con_add_failure');
					break;
			}
		} 
		$this->layout->adminview('form_addedit', $view);
	}
	function payment()
	{
		$this->layout->adminview('payment', $view);
	}

	function edit($id = '', $cur_page='')
	{
		$view                 = $this->getPath(__FUNCTION__);
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base').'upload_form/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$records = $this->form_model->getforms(0,1, array('id' => $id));
			$form = $records->records[0];
			if(empty($form)) 
			{
				$this->session->set_flashdata('form_msg', 'form_invalid_record');
				redirect($this->config->item('admin_base').'form/index','location');
				exit;
			}
			$view['id']		                = $id;
			
			$view['form']	                = $form;
		}
		
		$view['title']                 = "Edit form ";
		$view['page_title']            = "Edit form";
		$view['subtitle']	           = 'Edit form';
		$save                          = $this->input->post('save');
		if(!empty($save)) 
		{
			$pdf_file            	   = $_FILES['file']['name'];
			$ext                 	   = end(explode('.', $pdf_file));
			$data                      = $this->retrieve->post($this->form_model->fields);
			$insertStatus              = $this->form_model->udpate($id, $data);
			/************** pdf file upload*****************************/
			if (!($_FILES['file']['type'] =="application/pdf"))
			{
				echo "Your uploaded file must be of PDF. Other file types are not allowed<BR>";
			}
			else
			{
			
				$l_image_path           = "fpanel/images/uploads/".$id.".".$ext; // the path with the file name where the file will be stored,  
		
				if(move_uploaded_file ($_FILES['file']['tmp_name'],$l_image_path))
				{
				chmod("$l_image_path",0777);
				}
				else
				{
					echo "Failed to upload file Contact Site admin to fix the problem";
exit;
				}
			}
			/***************************************************************/
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('form_msg', 'form_edit_success');
					redirect($this->config->item('admin_base').'upload_form/index/'.$cur_page,'location');
					exit;		
					break;
				case -1:
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('form_already_exists');
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('form_valid_failed');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('form_edit_failure');
					break;
				;
				default:
					$view['msg'] 	= $this->lang->line('form_edit_failure');
					break;
			}
			$view['form'] 	       = $data;
		}
		$view['cur_page']              = $cur_page; 
		$this->layout->adminview('form_addedit', $view);
	}
	
	function cancel()
	{
    redirect($this->config->item('admin_base').'upload_form/index','location');
		exit;	
	}
	
	function delete($id = '', $cur_page)
	{
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base_url').'upload_form/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$deleteStatus = $this->form_model->delete($id);
			switch($deleteStatus)
			{
				case 1:
					$this->session->set_flashdata('users_msg', 'form_delete_success');
					redirect($this->config->item('admin_base').'upload_form/index/'.$cur_page,'location');
					exit;		
					break;
				case false:
					$view['msg'] 	    = $this->lang->line('form_delete_failure');
					break;
			}
		}
		
	}
	function clearFilter()
	{
		$this->session->unset_userdata('filter');
		redirect($this->config->item('admin_base').'upload_form/index/','location');
	}
}
?>